package com.thoughtworks.bytecode2class.constantpool;

import com.thoughtworks.bytecode2class.ClassReader;

public class FieldRefConstant extends Constant {

    private int classIndex;
    private int nameAndTypeIndex;

    public FieldRefConstant(ClassReader classReader) {
        super(classReader);
        classIndex = classReader.forwardChar();
        nameAndTypeIndex = classReader.forwardChar();
    }

    public void description() {
        System.out.printf("[%d]FieldRefConstant - [Class:%s, Name:%s, Descriptor:%s]%n",
                getCount(), getClassName(), getName(), getDescriptor());
    }

    public String getClassName() {
        ClassConstant constant = (ClassConstant) classReader.getConstantPool().get(classIndex);
        return constant.getName();
    }

    public String getName() {
        NameAndTypeConstant constant = (NameAndTypeConstant) classReader.getConstantPool().get(nameAndTypeIndex);
        return constant.getName();
    }

    public String getDescriptor() {
        NameAndTypeConstant constant = (NameAndTypeConstant) classReader.getConstantPool().get(nameAndTypeIndex);
        return constant.getDescriptor();
    }
}
